// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./financingDetailCost.css");
	var css_sample = require("./costtypes.css");
	var constant = require("constant");
	var tools = require("tools");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var financeId = "";
	var recieveNoticeId;
	 var html_cost_delece=require("./financingDetailCost-delect.html");
	 var html_createMember = require("./financingDetailCost-create.html");
	var enterpriseId = "";
	var amountI="";
	var financeType = "";
	var financeNumber = "";
	var dataFinancingDetail;
	var financeState;
	var storage = window.sessionStorage;
	var init = function() {
		initEvent();
		getParamList();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		
		
		$("#information").unbind("click").click(function() {
			$("#popdialog").html(html_createMember);
			dialog.showDialog($("#add_chareres_dialog"));
			$(".sure_yes").unbind("click").click(function() {
				newad();
			});
		});
		//点击修改
		$("#systemParamList").on("click", ".mod_button", function() {
			$(this).addClass("none");
			$(this).parent().find(".update_button").removeClass("none");
			$(this).parent().parent().find(".paramValue").css("border","1px solid #e2eaea").removeAttr("readonly");
		});
		//修改系统参数
		$("#systemParamList").on("click", ".update_button", function() {
			paramId = $(this).parent().parent().attr("paramId");
			paramValue = $(this).parent().parent().find(".paramValue").val();
			paramType = $(this).parent().parent().attr("paramType");
			updateParam();
			$(this).addClass("none");
			$(this).parent().find(".mod_button").removeClass("none");
			$(this).parent().parent().find(".paramValue").css("border","none").attr("readonly", "readonly");
		});
		
		
		//点击删除广告位通知
		$("#containercost-Body").on("click", ".costDelBtn", function() {
			$("#popdialog").html(html_cost_delece);
			var $this = $(this).parents("tr.tr_style");
			var charges_ID = [];
			charges_ID.push($this.attr("charges_ID"));
			deletead(charges_ID);
		});
		
	}
	  //修改系统参数
	var updateParam = function(){
        base.fnAjax({
            action: "updateSystemParam.json",
            oData: {
                paramValue: paramValue,
				paramId: paramId,
				paramType: paramType
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    base.alert(1,"修改成功！");
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    };
	
	// 增加
	var newad = function() {
	var promptDialog = validate.validateInput($("#add_chareres_dialog"));
	if (promptDialog.length != 0) {
		util.alert(promptDialog);
	} else {
		var body = base.parseForm($("#add_chareres_dialog"));
		body.charges_sort = $("#charges_sort").val();
		body.charges_name = $("#charges_name").val();
		body.curType = $("#curType").val();
		body.money = $("#money").val();
		body.application = $("#application").val();
		body.date = $("#date").val();
	
		body.example = imgSrc;
		base.fnAjax({
			action : "createCostApp.json", // 接口名称
			async : false, // 同步异步
			nocode : false, // 是否要登陆，true为不用登陆（其他页面不用传该参数，默认为false）
			oData : body, // 传递参数
			fnSuccess : function(data) { // 成功回调
				if (data.status == 200) {
					base.alert(1, "添加成功!");
					dialog.hideDialog($("#add_chareres_dialog"));
					router.route("financing/financingDetailCost.html");
				} else {
					base.alert(3, data.message);
				}
				dialog.hideDialog($("#add_chareres_dialog"));
			}
		});
	}
};
//获取贷款信息列表
var getParamList = function() {
	base.fnAjax({		//inquireSystemParamList
		action : "inquireCostList.json",
		oData : {
			pageIndex : pageIndex,
			pageSize : pageSize,
			/*paramName : paramName,
			paramType : 0*/
		}, // 传递参数
		fnSuccess : function(data) {
			if (data.status == 200) {
				var tpl = $("#financost-Detail").html();
				var html = juicer(tpl, data);
				$("#containercost-Body").html(html);
				totalPageCount = data.totalPageCount;
				totalRecordCount = data.totalRecordCount;
				base.pageControl($("#cost_pagenation"), 
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							getParamList();
						});
			} else {
				base.alert(2, "信息获取失败");
			}
		}
	});
};
//点击删除广告调用接口
var deletead = function(charges_ID) {
	var delete_ad_dialog = $("#delete_cost_one");
	dialog.showDialog(delete_ad_dialog, function() {});
	$(".footer-Btn").on("click", "#cost_delete——nuo", function() {
		base.fnAjax({
			action: "disableCostAmount.json", //接口名称
			oData: {
				charges_ID: charges_ID
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.alert(1, "删除成功！");
					router.route("financing/financingDetailCost.html");
				} else {
					base.alert(3, data.message);
				}
				dialog.hideDialog($("#delete_cost_one"));
			}
		});
	});
};
	return {
		init: init
	};
});